use crate::record;

type Format = Box<dyn Fn(&record::Record) + Send>;

pub struct ConsoleAppender {
    format: Format,
}

impl ConsoleAppender {
    pub fn new() -> Self {
        Self {
            format: Box::new(|record| {
                println!("{}", record.content);
            }),
        }
    }

    pub fn with_format<F>(mut self, format: F) -> Self
    where
        F: Fn(&record::Record) + 'static + Send,
    {
        self.format = Box::new(format);
        self
    }

    pub fn build(self) -> super::Appender {
        super::Appender::ConsoleAppender(self)
    }

    pub fn name(&self) -> &str {
        "console_appender"
    }

    pub fn process(&mut self, record: &record::Record) -> crate::Result<()> {
        (self.format)(record);

        Ok(())
    }
}

impl PartialEq for ConsoleAppender {
    fn eq(&self, _other: &Self) -> bool {
        true
    }
}

impl Default for ConsoleAppender {
    fn default() -> Self {
        Self::new()
    }
}
